package com.ymatou.iapi.cardservice.testcase;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.cardservice.parameter.AccountingItemModel;
import com.ymatou.iapi.cardservice.parameter.BizCodeEnum;
import com.ymatou.iapi.cardservice.parameter.CardInfo;
import com.ymatou.iapi.cardservice.parameter.FundoutEvidenceModel;
import com.ymatou.iapi.cardservice.parameter.StatusCodeEnum;
import com.ymatou.iapi.cardservice.service.AccountingCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.CardCallService;
import com.ymttest.database.annotation.SqlMapperFactory;
import com.ymttest.database.annotation.cardservice.CardServiceMapper;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.TestDataManager;
import com.ymttest.utils.YMTDateUtil;

public class Ts_Accounting {
	
	private static AccountingCall accountingCall = new AccountingCall();
	private static CardServiceMapper cardServiceMapper = SqlMapperFactory.getCardServiceMapper();
	private static String cardId = CardInfo.CardId1;
	private static String cardId_pswd = CardInfo.CardId1_Pswd;
	
	private static String cardId2 = CardInfo.CardId2;
	private static String cardId2_pswd = CardInfo.CardId2_Pswd;
	
	private static int userId = CardInfo.userId;
	private static String userName = CardInfo.userName;
	
	//卡初始金额
	private double denomination = 100.22;
	
	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("预付卡出入账");
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(accountingCall.getOpurl());
	}
	
	@After
	public void caseDown() {
		Logger.end(); 
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_001() throws Exception {
		Logger.start(true,"预付卡扣款");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(denomination);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm);
			checkAmount(MapUtil.hashMap(cardId,0d));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_001_01() throws Exception {
		Logger.start(true,"多次预付卡扣款");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			
			//第一次扣款 
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(10d);
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm);
			checkAmount(MapUtil.hashMap(cardId,denomination-10));
			
			
			
			//第二次扣款
			AccountingItemModel atm2 = getPaymentAccountingItemModel();
			atm2.setAmount(denomination-10);
			ret = accountingCall.call(Arrays.asList(atm2));
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm2);
			checkAmount(MapUtil.hashMap(cardId,0d));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_001_02() throws Exception {
		Logger.start(true,"一次请求多次预付卡扣款");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			
			//一次扣款多次款 
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(10d);
			
			
			AccountingItemModel atm2 = getPaymentAccountingItemModel();
			atm2.setAmount(denomination-12);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm,atm2));
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			
			checkAmount(MapUtil.hashMap(cardId,2d));
			checkCardEntry(Arrays.asList(atm,atm2));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_001_03() throws Exception {
		Logger.start(true,"一次请求多卡预付卡扣款");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			CardCallService.bindingCard(cardId2, cardId2_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId2, denomination);
			
			
			//一次扣款多次款 多张卡 
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(10d);
			
			
			AccountingItemModel atm2 = getPaymentAccountingItemModel();
			atm2.setCardId(cardId2);
			atm2.setAmount(12d);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm,atm2));
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			
			checkAmount(MapUtil.hashMap(cardId,denomination-10,cardId2,denomination-12));
			
			checkCardEntry(Arrays.asList(atm,atm2));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_002() throws Exception {
		Logger.start(true,"预付卡还款");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(denomination);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm);
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			//还款
			AccountingItemModel atm2 = getRefundAccountingItemModel(atm);
			ret = accountingCall.call(Arrays.asList(atm2));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm2);
			checkAmount(MapUtil.hashMap(cardId,denomination));
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_002_01() throws Exception {
		Logger.start(true,"预付卡多次还款");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(denomination);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm);
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			//还款1
			AccountingItemModel atm2 = getRefundAccountingItemModel(atm);
			atm2.setAmount(10d);
			ret = accountingCall.call(Arrays.asList(atm2));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm2);
			checkAmount(MapUtil.hashMap(cardId,10d));
			
			//还款2
			AccountingItemModel atm3 = getRefundAccountingItemModel(atm);
			atm3.setAmount(20d);
			ret = accountingCall.call(Arrays.asList(atm3));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm3);
			checkAmount(MapUtil.hashMap(cardId,30d));
			
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_002_02() throws Exception {
		Logger.start(true,"一次请求内预付卡多次还款");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(denomination);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm);
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			//还款1
			AccountingItemModel atm2 = getRefundAccountingItemModel(atm);
			atm2.setAmount(10d);
			AccountingItemModel atm3 = getRefundAccountingItemModel(atm);
			atm3.setAmount(20d);
			
			ret = accountingCall.call(Arrays.asList(atm2,atm3));
			
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
		
			checkCardEntry(Arrays.asList(atm2,atm3));
			checkAmount(MapUtil.hashMap(cardId,30d));
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_002_03() throws Exception {
		Logger.start(true,"一次请求内预付卡多张卡次还款");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			CardCallService.bindingCard(cardId2, cardId2_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId2, denomination);
			
			
			//一次扣款多次款 多张卡 
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(10d);
			
			AccountingItemModel atm2 = getPaymentAccountingItemModel();
			atm2.setCardId(cardId2);
			atm2.setAmount(12d);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm,atm2));
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkAmount(MapUtil.hashMap(cardId,denomination-10,cardId2,denomination-12));
			
			checkCardEntry(Arrays.asList(atm,atm2));
			
			//一次返款多次款 多张卡
			
			//还款1
			AccountingItemModel atm3 = getRefundAccountingItemModel(atm);
			atm3.setAmount(10d);
			AccountingItemModel atm4 = getRefundAccountingItemModel(atm2);
			atm4.setAmount(12d);
			
			ret = accountingCall.call(Arrays.asList(atm3,atm4));
			
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
		
			checkCardEntry(Arrays.asList(atm3,atm4));
			checkAmount(MapUtil.hashMap(cardId,denomination,cardId2,denomination));
			
			
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_003() throws Exception {
		Logger.start(true,"预付卡 扣费大于余额");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			CardCallService.bindingCard(cardId2, cardId2_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId2, denomination);
			
			
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(20d);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm);
			checkAmount(MapUtil.hashMap(cardId,denomination-20d));
			
			
			//第二次扣费
			AccountingItemModel atm2 = getPaymentAccountingItemModel();
			atm2.setAmount(denomination-10d);
			
			AccountingItemModel atm3 = getPaymentAccountingItemModel();
			atm3.setAmount(denomination);
			atm3.setCardId(cardId2);
			
			ret = accountingCall.call(Arrays.asList(atm2,atm3));
			
			Logger.verifyEquals(StatusCodeEnum.AvailbeInsufficient.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			
			checkAmount(MapUtil.hashMap(cardId,denomination-20d,cardId2,denomination));
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_003_01() throws Exception {
		Logger.start(true,"一次请求多次扣费 预付卡 扣费大于余额");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(20d);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm);
			checkAmount(MapUtil.hashMap(cardId,denomination-20d));
			
			
			AccountingItemModel atm2 = getPaymentAccountingItemModel();
			atm2.setAmount(10d);
			AccountingItemModel atm3 = getPaymentAccountingItemModel();
			atm3.setAmount(denomination-10d);
			
			ret = accountingCall.call(Arrays.asList(atm2,atm3));
			
			Logger.verifyEquals(StatusCodeEnum.AvailbeInsufficient.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			
			checkAmount(MapUtil.hashMap(cardId,denomination-20d));
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_004() throws Exception {
		Logger.start(true,"预付卡 还款大于面额");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(5d);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm);
			checkAmount(MapUtil.hashMap(cardId,denomination-5d));
			
			//还款1
			AccountingItemModel atm2 = getRefundAccountingItemModel(atm);
			atm2.setAmount(10d);
			
			ret = accountingCall.call(Arrays.asList(atm2));
			
			
			Logger.verifyEquals(StatusCodeEnum.RefundAmountInvalide.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			
		
			checkAmount(MapUtil.hashMap(cardId,denomination-5d));
			
			
		}catch(Exception e){
			Logger.fail(e); 
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_004_01() throws Exception {
		Logger.start(true,"一次请求多次还款 预付卡 还款大于面额");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(15d);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm);
			checkAmount(MapUtil.hashMap(cardId,denomination-15d));
			
			//还款1
			AccountingItemModel atm2 = getRefundAccountingItemModel(atm);
			atm2.setAmount(10d);
			AccountingItemModel atm3 = getRefundAccountingItemModel(atm);
			atm3.setAmount(10d);
			
			ret = accountingCall.call(Arrays.asList(atm2,atm3));
			
			
			Logger.verifyEquals(StatusCodeEnum.RefundAmountInvalide.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			
		
			checkAmount(MapUtil.hashMap(cardId,denomination-15d));
			
			
		}catch(Exception e){
			Logger.fail(e); 
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_005() throws Exception {
		Logger.start(true,"预付卡扣款 用户卡号对不上");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(denomination);
			atm.setUserId(userId+1);
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.InvalideOperation.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			
			checkAmount(MapUtil.hashMap(cardId,denomination));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_005_01() throws Exception {
		Logger.start(true,"预付卡还款 用户卡号对不上");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(denomination);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm);
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			//还款
			AccountingItemModel atm2 = getRefundAccountingItemModel(atm);
			atm2.setUserId(userId+1);
			ret = accountingCall.call(Arrays.asList(atm2));
			
			Logger.verifyEquals(StatusCodeEnum.InvalideOperation.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_006() throws Exception {
		Logger.start(true,"预付卡扣款 卡过期");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			cardServiceMapper.updateCardEndTimeByCardId(cardId, YMTDateUtil.getBeforeOrNextDate(-1));
			
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(denomination);
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.InvalideOperation.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			
			checkAmount(MapUtil.hashMap(cardId,denomination));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_006_01() throws Exception {
		Logger.start(true,"预付卡还款 卡过期");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(denomination);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm);
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			//还款
			cardServiceMapper.updateCardEndTimeByCardId(cardId, YMTDateUtil.getBeforeOrNextDate(-1));
			AccountingItemModel atm2 = getRefundAccountingItemModel(atm);
			ret = accountingCall.call(Arrays.asList(atm2));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm2);
			checkAmount(MapUtil.hashMap(cardId,denomination));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_007() throws Exception {
		Logger.start(true,"预付卡扣款 卡未绑定 ");
		try{
			//初始化卡，和金额
			cardServiceMapper.updateCardAsOrigByCardId(cardId);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(denomination);
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.InvalideOperation.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			
			checkAmount(MapUtil.hashMap(cardId,denomination));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_008() throws Exception {
		Logger.start(true,"预付卡扣款 一次请求重复指令");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(10d);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm,atm));
			
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			
			checkAmount(MapUtil.hashMap(cardId,denomination));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_008_01() throws Exception {
		Logger.start(true,"预付卡扣款 多次请求重复指令");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(10d);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm);
			checkAmount(MapUtil.hashMap(cardId,denomination-10d));
			
			
			
			ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.FullIdempotencyException.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			
			checkAmount(MapUtil.hashMap(cardId,denomination-10d));
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_008_02() throws Exception {
		Logger.start(true,"预付卡还款  一次请求重复指令");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(denomination);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm);
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			//还款
			AccountingItemModel atm2 = getRefundAccountingItemModel(atm);
			atm2.setAmount(10d);
			ret = accountingCall.call(Arrays.asList(atm2,atm2));
			
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_008_03() throws Exception {
		Logger.start(true,"预付卡还款 多请求重复指令");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(denomination);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm);
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			//还款
			AccountingItemModel atm2 = getRefundAccountingItemModel(atm);
			atm2.setAmount(10d);
			ret = accountingCall.call(Arrays.asList(atm2));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkAmount(MapUtil.hashMap(cardId,10d));
			
			//第二次还款
			ret = accountingCall.call(Arrays.asList(atm2));
			
			Logger.verifyEquals(StatusCodeEnum.FullIdempotencyException.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			
			checkAmount(MapUtil.hashMap(cardId,10d));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_009() throws Exception {
		Logger.start(true,"预付卡扣款 数据缺失");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			//缺appId
			AccountingItemModel atm = getPaymentAccountingItemModel();
			JSONObject ret = accountingCall.call(Arrays.asList(atm),"");
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,denomination));
			
			//卡号不存在
			atm = getPaymentAccountingItemModel();
			atm.setCardId("21312321");
			ret = accountingCall.call(Arrays.asList(atm));
			Logger.verifyEquals(StatusCodeEnum.CardNotExit.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,denomination));
			
			
			atm = getPaymentAccountingItemModel();
			atm.setCardId("");
			ret = accountingCall.call(Arrays.asList(atm));
			Logger.verifyEquals(StatusCodeEnum.CardNotExit.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,denomination));
			
			
			//userid不存在
			atm = getPaymentAccountingItemModel();
			atm.setUserId(0);
			ret = accountingCall.call(Arrays.asList(atm));
			Logger.verifyEquals(StatusCodeEnum.InvalideOperation.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,denomination));
			
			
			
			//memo不存在
			atm = getPaymentAccountingItemModel();
			atm.setMemo("");
			ret = accountingCall.call(Arrays.asList(atm));
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,denomination));
			
			
			//bizCode不在范围
			atm = getPaymentAccountingItemModel();
			atm.setBizCode(1001);
			ret = accountingCall.call(Arrays.asList(atm));
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,denomination));
			
			
			//orignalNo不存在
			atm = getPaymentAccountingItemModel();
			atm.setBizNo("");
			ret = accountingCall.call(Arrays.asList(atm));
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,denomination));
			
			
			//bizNo不存在
			atm = getPaymentAccountingItemModel();
			atm.setOriginalNo("");
			ret = accountingCall.call(Arrays.asList(atm));
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,denomination));
			
			
			//amount=0
			atm = getPaymentAccountingItemModel();
			atm.setAmount(0);
			ret = accountingCall.call(Arrays.asList(atm));
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,denomination));
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	

	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_Accounting_009_01() throws Exception {
		Logger.start(true,"预付卡还款 数据缺失");
		try{
			//初始化卡，和金额
			CardCallService.bindingCard(cardId, cardId_pswd, userId, userName);
			cardServiceMapper.updateCardAmountByCardId(cardId, denomination);
			
			AccountingItemModel atm = getPaymentAccountingItemModel();
			atm.setAmount(denomination);
			
			JSONObject ret = accountingCall.call(Arrays.asList(atm));
			
			Logger.verifyEquals(StatusCodeEnum.Success.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(true, ret.getBoolean("success"), "check success");
			
			checkCardEntry(atm);
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			//还款
			//appId缺失
			AccountingItemModel atm2 = getRefundAccountingItemModel(atm);
			ret = accountingCall.call(Arrays.asList(atm2),"");
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			////卡号不存在
			atm2 = getRefundAccountingItemModel(atm);
			atm2.setCardId("12312312");
			ret = accountingCall.call(Arrays.asList(atm2));
			Logger.verifyEquals(StatusCodeEnum.CardNotExit.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			//userid不存在
			atm2 = getRefundAccountingItemModel(atm);
			atm2.setUserId(userId+1);
			ret = accountingCall.call(Arrays.asList(atm2));
			Logger.verifyEquals(StatusCodeEnum.InvalideOperation.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			//memo不存在
			atm2 = getRefundAccountingItemModel(atm);
			atm2.setMemo("");
			ret = accountingCall.call(Arrays.asList(atm2));
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			//bizCode不在范围
			atm2 = getRefundAccountingItemModel(atm);
			atm2.setBizCode(10001);
			ret = accountingCall.call(Arrays.asList(atm2));
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			//orignalNo不存在
			atm2 = getRefundAccountingItemModel(atm);
			atm2.setOriginalNo("");
			ret = accountingCall.call(Arrays.asList(atm2));
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			//bizNo不存在
			atm2 = getRefundAccountingItemModel(atm);
			atm2.setBizNo("");
			ret = accountingCall.call(Arrays.asList(atm2));
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			//amount=0
			atm2 = getRefundAccountingItemModel(atm);
			atm2.setAmount(0);
			ret = accountingCall.call(Arrays.asList(atm2));
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			
			
			//fundoutEvidence节点  不存在
			atm2 = getRefundAccountingItemModel(atm);
			atm2.setFundoutEvidence(null);
			ret = accountingCall.call(Arrays.asList(atm2));
			Logger.verifyEquals(StatusCodeEnum.ArgumentException.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,0d));
			
			
	
			//fundoutEvidence -> bizNo不存在
			atm2 = getRefundAccountingItemModel(atm);
			atm2.getFundoutEvidence().setBizNo("123");
			ret = accountingCall.call(Arrays.asList(atm2));
			Logger.verifyEquals(StatusCodeEnum.InvalideOperation.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,0d));			
			
			
			//fundoutEvidence -> bizCode不存在
			atm2 = getRefundAccountingItemModel(atm);
			atm2.getFundoutEvidence().setBizCode(1234);
			ret = accountingCall.call(Arrays.asList(atm2));
			Logger.verifyEquals(StatusCodeEnum.InvalideOperation.getCode(), ret.getInt("statusCode"), "check statusCode");
			Logger.verifyEquals(false, ret.getBoolean("success"), "check success");
			checkAmount(MapUtil.hashMap(cardId,0d));			
			
			
			//fundoutEvidence -> originalNo 该字段还款时 不使用
			
		}catch(Exception e){
			Logger.fail(e);
		}
	}
	
	
	// 扣款model
	public static AccountingItemModel getPaymentAccountingItemModel() {
		AccountingItemModel atm = new AccountingItemModel();
		atm.setCardId(cardId);
		atm.setUserId(userId);
		atm.setAmount(10);
		atm.setOperator(TestDataManager.randomStr(6));
		atm.setMemo(TestDataManager.randomStr(6));
		atm.setOriginalNo(TestDataManager.randomStr(10));
		atm.setBizNo(atm.getOriginalNo());
		atm.setBizCode(BizCodeEnum.Payment.getCode());
		atm.setAccountOperateType(2);
		return atm;
	}
	
	
	
	// 还款model
	public static AccountingItemModel getRefundAccountingItemModel(AccountingItemModel paymentModel) {
		AccountingItemModel atm = new AccountingItemModel();
		atm.setCardId(paymentModel.getCardId());
		atm.setUserId(paymentModel.getUserId());
		atm.setAmount(paymentModel.getAmount());
		atm.setOperator(TestDataManager.randomStr(6));
		atm.setMemo(TestDataManager.randomStr(6));
		atm.setOriginalNo(paymentModel.getOriginalNo());
		atm.setBizNo(TestDataManager.randomStr(10));
		atm.setBizCode(BizCodeEnum.Refund.getCode());
		atm.setAccountOperateType(1);
		
		FundoutEvidenceModel fem = new FundoutEvidenceModel();
		fem.setBizCode(BizCodeEnum.Payment.getCode());
		fem.setOriginalNo(paymentModel.getOriginalNo());
		fem.setBizNo(paymentModel.getBizNo());
		
		atm.setFundoutEvidence(fem);
		return atm;
	}
		
		
	
	public static void checkAmount(Map<String,Double> cardAndAmount){
		cardAndAmount.keySet().forEach(cardId->{
			double amountInDb = Double.parseDouble(String.valueOf(cardServiceMapper.selectCardByCardId(cardId).get("AvailAmount")));
			Logger.verifyEquals(cardAndAmount.get(cardId), amountInDb, "check amount card:["+ cardId+"]");
		});
	}
	
	
	public static void checkCardEntry(AccountingItemModel atm){
		checkCardEntry(Arrays.asList(atm));
	}
	
	
	public static void checkCardEntry(List<AccountingItemModel> atmList){
		for(AccountingItemModel atm:atmList){
			Map<String,Object> cardEntryMap = cardServiceMapper.selectCardEntryByCardIdAndBizCodeAndBizNo(atm.getCardId(), atm.getBizCode(), atm.getBizNo());
			double amount = atm.getAccountOperateType()==2?atm.getAmount()*(-1):atm.getAmount();
			Logger.verifyEquals(amount, Double.parseDouble(String.valueOf(cardEntryMap.get("Amount"))), "check amount");
			Logger.verifyEquals(atm.getUserId(), Integer.parseInt(String.valueOf(cardEntryMap.get("UserId"))), "check UserId");
			Logger.verifyEquals(atm.getCardId(), String.valueOf(cardEntryMap.get("CardId")), "check cardId");
			Logger.verifyEquals(atm.getMemo(), String.valueOf(cardEntryMap.get("Memo")), "check Memo");
			Logger.verifyEquals(atm.getOriginalNo(),String.valueOf(cardEntryMap.get("OriginalNo")), "check OriginalNo");
			Logger.verifyEquals(atm.getOperator(),String.valueOf(cardEntryMap.get("Operator")), "check Operator");
			
		}
		
	}
	
}
